#在docker中的脚本，前面不需要sudo，注意要传入ip
#! /bin/sh
if [ ! -n "$1" ] ;then

    echo "you have not input a IP!"
else

    echo "the IP you input is $1"
	user root;
	echo '####################开始FastDfs自动部署####################'
	basePath=/usr/local ##指定安装包的位置

	yum -y install wget 
	yum -y install gcc gcc-c++
	yum -y install perl
	yum -y install unzip
	yum -y install make

	#部署libfastcommon包
	cd $basePath
	if [ ! -f "V1.0.7.tar.gz" ]; then
		wget https://github.com/happyfish100/libfastcommon/archive/V1.0.7.tar.gz
	fi
	if [ ! -f "libfastcommon-1.0.7" ]; then
		tar -zxf V1.0.7.tar.gz
	fi
	cd libfastcommon-1.0.7
	./make.sh
	./make.sh install
	#创建软链接
	rm -rf /usr/local/lib/libfastcommon.so /usr/lib/libfastcommon.so /usr/local/lib/libfdfsclient.so /usr/lib/libfdfsclient.so
	ln -s /usr/lib64/libfastcommon.so /usr/local/lib/libfastcommon.so
	ln -s /usr/lib64/libfastcommon.so /usr/lib/libfastcommon.so
	ln -s /usr/lib64/libfdfsclient.so /usr/local/lib/libfdfsclient.so
	ln -s /usr/lib64/libfdfsclient.so /usr/lib/libfdfsclient.so

	#部署fastdfs-5.05包
	cd $basePath
	if [ ! -f "V5.05.tar.gz" ]; then
		wget https://github.com/happyfish100/fastdfs/archive/V5.05.tar.gz
	fi
	if [ ! -f "fastdfs-5.05" ]; then
		tar -zxf V5.05.tar.gz
	fi
	cd fastdfs-5.05
	./make.sh
	./make.sh install
	#复制
	cd /etc/fdfs
	rm -rf tracker.conf storage.conf client.conf
	cp tracker.conf.sample tracker.conf
	cp storage.conf.sample storage.conf
	cp client.conf.sample client.conf
	#替换tracker.conf
	sed -i "s/^bind_addr.*/bind_addr=/g" /etc/fdfs/tracker.conf
	sed -i "s/^base_path=.*/base_path=\/fastdfs\/tracker/g" /etc/fdfs/tracker.conf
	sed -i "s/^port=22122.*/port=22122/g" /etc/fdfs/tracker.conf
	sed -i "s/^http.server_port=.*/http.server_port=8080/g" /etc/fdfs/tracker.conf
	#替换storage.conf
	sed -i "s/^group_name=.*/group_name=files/g" /etc/fdfs/storage.conf
	sed -i "s/^bind_addr=.*/bind_addr=/g" /etc/fdfs/storage.conf
	sed -i "s/^port=23000.*/port=23000/g" /etc/fdfs/storage.conf
	sed -i "s/^base_path=.*/base_path=\/fastdfs\/storage\/info/g" /etc/fdfs/storage.conf
	sed -i "s/^store_path0=.*/store_path0=\/fastdfs\/storage/g" /etc/fdfs/storage.conf
	sed -i "s/^tracker_server=.*/tracker_server=$1:22122/g" /etc/fdfs/storage.conf
	sed -i "s/^http.server_port=.*/http.server_port=8888/g" /etc/fdfs/storage.conf
	#替换client.conf
	sed -i "s/^tracker_server=.*/tracker_server=$1:22122/g" /etc/fdfs/client.conf
	sed -i "s/^base_path=.*/base_path=\/fastdfs\/storage/g" /etc/fdfs/client.conf

	#创建目录
	rm -rf /fastdfs
	mkdir -p /fastdfs/tracker
	mkdir -p /fastdfs/storage/info

	#启动tracker与storage
	fdfs_trackerd  /etc/fdfs/tracker.conf start
	fdfs_storaged  /etc/fdfs/storage.conf start

	#部署fastdfs-nginx-module_v1.16
	cd $basePath
	if [ ! -f "fastdfs-nginx-module_v1.16.tar.gz" ]; then
		wget http://jaist.dl.sourceforge.NET/project/fastdfs/FastDFS%20Nginx%20Module%20Source%20Code/fastdfs-nginx-module_v1.16.tar.gz
	fi
	rm -rf fastdfs-nginx-module
	tar -zxf fastdfs-nginx-module_v1.16.tar.gz
	cd /usr/local/fastdfs-nginx-module/src
	#替换config
	sed -i "s/^CORE_INCS=.*/CORE_INCS=\"\$CORE_INCS \/usr\/include\/fastdfs \/usr\/include\/fastcommon\/\"/g" /usr/local/fastdfs-nginx-module/src/config
	#复制mod_fastdfs.conf、http.conf、mime.types
	cp  /usr/local/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs
	cp /usr/local/fastdfs-5.05/conf/http.conf  /etc/fdfs/
	cp /usr/local/fastdfs-5.05/conf/mime.types  /etc/fdfs/
	#创建目录
	mkdir -p /fastdfs/nginxlog
	#替换mod_fastdfs.conf
	sed -i "s/^group_name=.*/group_name=files/g" /etc/fdfs/mod_fastdfs.conf
	sed -i "s/^tracker_server=.*/tracker_server=$1:22122/g" /etc/fdfs/mod_fastdfs.conf
	sed -i "s/^store_path0=.*/store_path0=\/fastdfs\/storage/g" /etc/fdfs/mod_fastdfs.conf
	sed -i "s/^base_path=.*/base_path=\/fastdfs\/nginxlog/g" /etc/fdfs/mod_fastdfs.conf
	sed -i "s/^url_have_group_name.*/url_have_group_name=true/g" /etc/fdfs/mod_fastdfs.conf
	cd /fastdfs
	ln -s /fastdfs/storage/data /fastdfs/data

	#部署pcre-8.36.tar.gz
	cd $basePath
	if [ ! -f "pcre-8.36.tar.gz" ]; then
		wget https://ftp.pcre.org/pub/pcre/pcre-8.36.tar.gz
	fi
	tar -zxvf pcre-8.36.tar.gz
	cd pcre-8.36
	./configure
	make 
	make install
	ln -s /usr/local/lib/libpcre.so.1 /lib64/	

	#部署zlib-1.2.11.tar.gz
	cd $basePath
	if [ ! -f "zlib-1.2.11.tar.gz" ]; then
		wget http://zlib.Net/zlib-1.2.11.tar.gz
	fi
	tar -zxvf zlib-1.2.11.tar.gz
	cd zlib-1.2.11
	./configure
	make 
	make install

	#部署nginx-1.8.0.tar.gz
	basePath=/usr/local
	cd $basePath
	if [ ! -f "nginx-1.8.0.tar.gz" ]; then
		wget http://mirrors.sohu.com/nginx/nginx-1.8.0.tar.gz
	fi
	tar -zxvf nginx-1.8.0.tar.gz
	cd nginx-1.8.0
	./configure --prefix=/usr/local/nginx --add-module=/usr/local/fastdfs-nginx-module/src
	make
	make install
	#替换nginx.conf
	cd /usr/local/nginx/conf
	sed -i '47a         location /files/M00{\n            root /fastdfs/data;\n            ngx_fastdfs_module;\n        }' /usr/local/nginx/conf/nginx.conf

	/usr/local/nginx/sbin/nginx
	echo '####################FastDfs自动部署完成####################'
fi

